package com.uchengwang.ui.cm_ui.umsg.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.AppCompatCheckBox;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.listener.HttpCallBack;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.base_ui.ActivityTitleBar;
import com.uchengwang.ui.base_ui.ui.AllBaseActivity;
import com.uchengwang.ui.cm_ui.login.ui.AuthenStateActivity;
import com.uchengwang.ui.utils.GetInfoUtils;
import com.uchengwang.ui.utils.SPUtils;
import com.uchengwang.ui.utils.UCommonUtil;
import com.uchengwang.ui.view.CircleImageView;

import java.io.File;

/**
 * Created by U城网 on 2017/6/6.
 * 社团认证
 */

public class ClubAuthenActivity extends AllBaseActivity implements View.OnClickListener{
    private EditText admin;
    private Button next;
    private TextView name;
    private TextView state, school, clubType;
    private CircleImageView head;
    private ActivityTitleBar titleBar;

    private String headUrl, nameString, headUrlOss, adminString, schoolString, typeString;
    private AppCompatCheckBox checkBox;
    private long orgAuthenId;//社团ID
    private String orgType;

    private int type;//根据type跳转不同的界面 0默认跳转到MainActivity  1跳转到社团活动报名页

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_club_authen);
        type = getIntent().getIntExtra(BaseConfig.TYPE, 0);

        initView();
        titleBar.setTitle("社团认证", 0);
    }
    
    private void initView(){
        titleBar = (ActivityTitleBar) findViewById(R.id.clubAuthen_titleBar);
        name = (TextView) findViewById(R.id.clubAuthen_name);
        name.setOnClickListener(this);
        school = (TextView) findViewById(R.id.clubAuthen_school);

        clubType = (TextView) findViewById(R.id.clubAuthen_clubType);
        clubType.setOnClickListener(this);
        admin = (EditText) findViewById(R.id.clubAuthen_admin);

        next = (Button) findViewById(R.id.clubAuthen_next);
        next.setOnClickListener(this);

        state = (TextView) findViewById(R.id.clubAuthen_state);//使用说明
        state.setOnClickListener(this);

        head = (CircleImageView) findViewById(R.id.clubAuthen_head);
        head.setOnClickListener(this);

        checkBox = (AppCompatCheckBox) findViewById(R.id.clubAuthen_checkBox);
        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b){
                    next.setEnabled(true);
                    next.setBackgroundResource(R.drawable.selector_login_new);
                }else {
                    next.setEnabled(false);
                    next.setBackgroundResource(R.color.comment_color);
                }
            }
        });


        if (UCommonUtil.getUtil().isLogin()){
            adminString = SPUtils.getPreferences(this).getRealName();
            if (!TextUtils.isEmpty(adminString)){
                admin.setText(adminString);
            }
        }

        if (!TextUtils.isEmpty(UChengApplication.mySchoolName)){
            school.setText(UChengApplication.mySchoolName);
        }
    }

    @Override
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()){
            case R.id.clubAuthen_name://选择社团名称
                intent  = new Intent(this, ClubsActivity.class);
                startActivityForResult(intent, 0);
                break;
            case R.id.clubAuthen_head:
                chooseImage();
                break;
            case R.id.clubAuthen_state:
                intent = new Intent(this, AuthenStateActivity.class);
                intent.putExtra(BaseConfig.TYPE, 3);
                startActivity(intent);
                break;
            case R.id.clubAuthen_clubType:
                //社团类型
                startActivityForResult(new Intent(this, ClubTypeActivity.class), 0);
                break;
            case R.id.clubAuthen_next:
                nameString = name.getText().toString();
                schoolString = school.getText().toString();
                typeString = clubType.getText().toString();
                adminString = admin.getText().toString();

                if (TextUtils.isEmpty(nameString)){
                    UCommonUtil.getUtil().showToast(ClubAuthenActivity.this, "社团名不能为空");
                    return;
                }
                if (TextUtils.isEmpty(headUrl)){
                    UCommonUtil.getUtil().showToast(ClubAuthenActivity.this, "为自己的社团选个logo吧");
                    return;
                }
                if (TextUtils.isEmpty(schoolString)){
                    UCommonUtil.getUtil().showToast(ClubAuthenActivity.this, "校区不能为空");
                    return;
                }
                if (TextUtils.isEmpty(orgType)){
                    UCommonUtil.getUtil().showToast(ClubAuthenActivity.this, "请选择社团类型");
                    return;
                }
                if (TextUtils.isEmpty(adminString)){
                    UCommonUtil.getUtil().showToast(ClubAuthenActivity.this, "社长不能为空");
                    return;
                }

                int clubType = GetInfoUtils.getUtils().getClubType(orgType);

                intent = new Intent(this, ClubAuthen2Activity.class);
                intent.putExtra(BaseConfig.ORG_NAME, nameString);
                intent.putExtra(BaseConfig.LOGO_IMG, headUrl);
                intent.putExtra(BaseConfig.UNI_NAME, schoolString);
                intent.putExtra(BaseConfig.ADMIN_NAME, adminString);
                intent.putExtra(BaseConfig.ORG_AUTH_ID, orgAuthenId);
                intent.putExtra(BaseConfig.PAR_CLASSI_FY_ID, clubType);
                intent.putExtra(BaseConfig.ORG_TYPE, orgType);
                intent.putExtra(BaseConfig.TYPE, type);

                startActivity(intent);
                this.finish();
                break;

        }

    }

    /**
     * 选择图片
     * */
    private void chooseImage(){
        UCommonUtil.getUtil().openAndCrop(this, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
            }
            @Override
            public void onResp(int status, String msg, String data) {
                if (!TextUtils.isEmpty(data)){
                    headUrl = data;
                    if (!TextUtils.isEmpty(headUrl)){
                        ImageManager.getInstance().loadImage(ClubAuthenActivity.this, head, new File(headUrl), ImageManager.ImageType.CIRCLE);
                    }
                }
            }
        });
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode ==0 && data != null){
            switch (resultCode){
                case 1:
                    orgType = data.getStringExtra(BaseConfig.TYPE);
                    clubType.setText(orgType);
                    break;
                case 2:
                    String clubName = data.getStringExtra(BaseConfig.ORG_NAME);
                    orgAuthenId = data.getLongExtra(BaseConfig.ORG_AUTH_ID, 0);
                    name.setText(clubName);
                    break;
            }

        }
    }
}
